package com.moyu.plugins.pokemsg.Listener;

import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.scheduler.BukkitRunnable;

import com.moyu.plugins.pokemsg.PokeMsg;
import com.pixelmonmod.pixelmon.api.events.CaptureEvent.SuccessfulCapture;
import com.pixelmonmod.pixelmon.entities.pixelmon.stats.IVStore;
import com.pixelmonmod.pixelmon.enums.EnumSpecies;

import catserver.api.bukkit.event.ForgeEvent;
import net.minecraft.entity.player.EntityPlayerMP;

public class CaptureListener implements Listener {
	
	private final PokeMsg plugin;
	
	public CaptureListener (PokeMsg plugin)
	{
		this.plugin = plugin;
	}
	
	public int getPercentage(IVStore ivs) 
	{
		return plugin.getPercentage(ivs);
	}

	public  FileConfiguration getConfig()
	{
		return plugin.getConfig();
	}
	
	
	@SuppressWarnings("deprecation")
	@EventHandler
	public void onSuccessfulCapture(ForgeEvent event) throws IllegalArgumentException, SecurityException {
		//成功捕捉事件
		if((event.getForgeEvent() instanceof SuccessfulCapture) && getConfig().getBoolean("Capture.enable"))
		{
			//获取捕捉事件e
			SuccessfulCapture e = (SuccessfulCapture) event.getForgeEvent();
			EntityPlayerMP player = e.player;
			
			
			String baseName = e.getPokemon().getSpecies().getPokemonName(),
					pokeName = e.getPokemon().getPokemonData().getDisplayName(),
					p = player.func_70005_c_(),//getName()
					ball = e.pokeball.getType().getItem().getLocalizedName();
			p = plugin.getChat().getPlayerPrefix(Bukkit.getPlayer(p)) + Bukkit.getPlayer(p).getName();
			
			int ivs = this.getPercentage(e.getPokemon().getPokemonData().getIVs());
			
			Boolean shiny = e.getPokemon().getPokemonData().isShiny(),
					legend = e.getPokemon().isLegendary();
			
			BossBar bar = Bukkit.createBossBar("", plugin.random(BarColor.class), BarStyle.SEGMENTED_20);

			
			//闪光神兽
			if(shiny && legend)
			{
				sendMsg(bar, "Shiny_Legendary", p, pokeName, ball, ivs);
			}
			//闪光
			else if(shiny)
			{
				sendMsg(bar, "Shiny", p, pokeName, ball, ivs);
			}
			//神兽
			else if(legend)
			{
				sendMsg(bar, "Legendary", p, pokeName, ball, ivs);
			}
			//普通
			else
			{
				sendMsg(bar, "Common", p, pokeName, ball, ivs);
			}
		}
	}

	
	public  void sendMsg(BossBar bar, String type, String p, String pokeName, String ball, int ivs) {
		
		if (getConfig().getBoolean("Capture." + type + ".chatbox"))
			Bukkit.broadcastMessage(this.bianLiang(getConfig().getString("Capture.message.chatbox.msg"), p, pokeName, ball, String.valueOf(ivs), getConfig().getString("Capture." + type + ".name")));
	
		if (getConfig().getBoolean("Capture." + type + ".bossbar"))
		{
			bar.setTitle(this.bianLiang(getConfig().getString("Capture.message.bossbar.msg"), p, pokeName, ball, String.valueOf(ivs), getConfig().getString("Capture." + type + ".name")));
			for (Player onlinep:Bukkit.getOnlinePlayers())
				bar.addPlayer(onlinep);
			
			new BukkitRunnable(){
			    @Override
			    public void run(){
			    	bar.setVisible(false);
			    	this.cancel();
			    }
			}.runTaskLaterAsynchronously(plugin, 20*getConfig().getInt("Capture.message.bossbar.time"));
		}
	}
	
	public String bianLiang(String str, String p, String poke, String ball,String ivs, String type) {
		
		if (str.contains("[player]"))
    		str=str.replace("[player]", p);
		
		if (str.contains("[pokemon]"))
    		str=str.replace("[pokemon]", poke);
		
		if (str.contains("[ball]"))
    		str=str.replace("[ball]", ball);
		
		if (str.contains("[ivs]"))
    		str=str.replace("[ivs]", ivs);
		
		if (str.contains("[type]"))
    		str=str.replace("[type]", type);
    	
		if (str.contains("&"))
    		str=str.replace("&", "§");
		
		return str;
		
	}

	
}
